package com.hunan.api.catv.service.biz.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.MemberReconciliationRecord;
import com.hunan.api.catv.entity.vo.MemberReconciliationRecordVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.impl.account.AccountReconciliationService;
import com.hunan.api.catv.utils.EasyExportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Author: wangchao
 * @Date: 2020/10/27 17:00
 */
@Service
public class AccountReconciliationBiz extends ResultService {

    @Autowired
    AccountReconciliationService accountReconciliationService;


    /**
     * 保存用户提现操作记录
     */
    public ServiceResult saveMemberReconciliationInfo(MemberReconciliationRecord memberReconciliationRecord) {
        boolean insert = memberReconciliationRecord.setCreateTime(new Date())
                .setUpdateTime(new Date())
                .setStatus(memberReconciliationRecord.getStatus() != null ? memberReconciliationRecord.getStatus() : 0).insert();
        if (!insert) {
            return buildFail(false, ConstantsMsg.admin_add_fail);
        }
        return buildOK(true, ConstantsMsg.admin_add_success);
    }


    public PageInfo<MemberReconciliationRecordVo> getReconciliationRecordList(int pageNum, int pageSize, String nickName, String startCreateTime, String endCreateTime, Integer memberAssertId, Integer status, String phone) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(getReconciliationList(nickName, startCreateTime, endCreateTime, memberAssertId, status, phone));
    }

    private List<MemberReconciliationRecordVo> getReconciliationList(String nickName, String startCreateTime, String endCreateTime, Integer memberAssertId, Integer status, String phone) {
        MemberReconciliationRecordVo memberReconciliationRecordVo = new MemberReconciliationRecordVo();
        memberReconciliationRecordVo.setNickName(nickName);
        memberReconciliationRecordVo.setMemberAssertId(memberAssertId);
        memberReconciliationRecordVo.setStartCreateTime(startCreateTime);
        memberReconciliationRecordVo.setEndCreateTime(endCreateTime);
        memberReconciliationRecordVo.setStatus(status);
        memberReconciliationRecordVo.setPhone(phone);
        return accountReconciliationService.getMemberReconciliationRecord(memberReconciliationRecordVo);
    }

    /**
     * 导出
     */
    public ServiceResult exportReconciliationRecord(String nickName, Integer memberAssertId, String startCreateTime, String endCreateTime, Integer status, String phone, HttpServletResponse response) {
        // 导出操作
        EasyExportUtils.exportExcel(getReconciliationList(nickName, startCreateTime, endCreateTime, memberAssertId, status, phone), "", "sheet1", MemberReconciliationRecordVo.class, "用户提现明细.xlsx", response);

        return buildOK(true);
    }

    /**
     * 查询该用户提现列表记录
     */
    public PageInfo<MemberReconciliationRecord> getMemberReconciliationList(int pageNum, int pageSize, Integer memberId) {
        PageHelper.startPage(pageNum, pageSize);
        List<MemberReconciliationRecord> memberReconciliationRecordList = new MemberReconciliationRecord().selectList(new QueryWrapper<MemberReconciliationRecord>()
                .eq(MemberReconciliationRecord.MEMBER_ID, memberId)
                .eq(MemberReconciliationRecord.TYPE, 1)
                .eq(MemberReconciliationRecord.STATUS, 1)
                .orderByDesc(MemberReconciliationRecord.CREATE_TIME)
        );
        return new PageInfo<>(memberReconciliationRecordList);
    }
}
